﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using static Dapper.SqlMapper;


namespace HBase {//
    public abstract class AbsBaseServer :IBaseServer {
        protected IDbConnection Connection { get { return getConnection(); } }
        protected abstract IDbConnection getConnection();
        abstract public GKDBType getGKDBType();
        public Page<T> queryPage<T>(string sql ,string orderBy ,PageDto param) {
            if( param == null || param.pageSize == -1 ) {
                List<T> list = getConnection().Query<T>(sql + " order by " + orderBy ,param).ToList<T>();
                Page<T> p = new Page<T>();
                p.content = list;
                return p;
            }
            int start = 0, end = 20;
            if( param != null ) {
                start = (param.pageNum - 1) * param.pageSize + 1;
                end = start + param.pageSize - 1;
            }
            if( string.IsNullOrEmpty(sql) ) {
                return null;
            }
            if( (start <= 0 && end <= 0) ) {
                return null;
            }
            sql = PageDialect.pageSql(getGKDBType() ,sql ,start ,end ,orderBy);
            Page<T> page = getConnection().QueryMultiple<T>(sql ,param);
            return page;
        }
        public List<T> GetAll<T>(string whereStr = null ,object param = null) where T : class {
            IDbConnection con = getConnection();
            List<T> list = con.GetAll<T>(whereStr ,param ,null);
            return list;
        }
        public List<T> getAll<T>(string tableName ,string whereStr = null ,object param = null ,string orderField = "") where T : class {
            string sql = "select * from " + tableName + " where 1=1 " + whereStr;
            if( !String.IsNullOrWhiteSpace(orderField) ) {
                sql += " order by " + orderField;
            }
            return getConnection().Query<T>(sql ,param)
                .ToList();
        }
        public List<T> GetField<T>(string fields ,string where = null ,object param = null) where T : class {
            return getConnection().getField<T>(fields ,where ,param);
        }
        public long insert<T>(T t) where T : class {
            return getConnection().Insert<T>(t);
        }
        public List<T> GetEntities<T>(string sql ,string connStr = "" ,params SqlParameter[] parameters) {
            var dp = new DynamicParameters();
            if( parameters != null && parameters.Length > 0 ) {
                foreach( SqlParameter pt in parameters ) {
                    dp.Add(pt.ParameterName ,pt.Value ,pt.DbType);
                }
            }
            var result = getConnection().Query<T>(sql ,dp);
            return result.ToList();
        }
        public T getById<T>(long id ,IDbTransaction transaction = null) where T : class {
            return getConnection().GetById<T>(id ,transaction);
        }
        public long insertNotNull<T>(T t) where T : class {
            return getConnection().InsertNoNull<T>(t);
        }
        public bool updateNotNull<T>(T t) where T : class {
            return getConnection().updateNotNull(t);
        }
        public bool update<T>(T t) where T : class {
            return getConnection().Update(t);
        }
        public void delete<T>(long id) where T : class {
            getConnection().Delete<T>(id);
        }
        public void delete(BaseEntity e) {

            getConnection().delete(e);

        }

        public void delete<T>(string where ,object obj) where T : class {

            Type type = typeof(T);
            var name = ToStr.GetTableName(type);
            var statement = String.Format("delete from {0} where {1} " ,name ,where);
            var deleted = getConnection().Execute(statement ,obj);


        }
    }
}
